package test.spring;

import java.util.List;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService;

public class AnnotatedConversionServiceBeanFactory implements InitializingBean, FactoryBean<ConversionService> {

	private ConfigurableConversionService conversionService;
	
	@Autowired(required = false)
	private List<IConverterRegistry> converterRegistries;

	@Override
	public void afterPropertiesSet() throws Exception {
		if (converterRegistries == null) {
			return;
		}
		for (IConverterRegistry registry : converterRegistries) {
			registry.register(conversionService);
		}

	}
	
	@Override
	public ConversionService getObject() throws Exception {
		return conversionService;
	}

	@Override
	public Class<?> getObjectType() {
		return ConversionService.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	/**
	 * @param conversionService
	 *            the conversionService to set
	 */
	public void setConversionService(ConfigurableConversionService conversionService) {
		this.conversionService = conversionService;
	}

}
